package com.nuanshui.heatedloan.search.biz.mq;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.framework.utils.serializer.SerializationUtils;
import com.nuanshui.heatedloan.middleware.rabbitmq.RabbitmqMessageHandler;
import com.nuanshui.heatedloan.middleware.rabbitmq.annotation.MqMessageHandler;
import com.nuanshui.heatedloan.search.biz.ElasticSearchServiceImpl;
import com.nuanshui.heatedloan.search.facade.ElasticSearchService;
import com.nuanshui.heatedloan.task.facade.model.TaskLog;

@MqMessageHandler("job.log.queue")
public class TaskLogHandler  implements RabbitmqMessageHandler {

	private Logger Logger = LoggerFactory.getLogger("task_log");
	
	@Autowired
	private ElasticSearchService elasticSearchService;

	@Override
	public void onMessage(Message message, Channel channel) throws Exception {
		String str = String.valueOf(SerializationUtils.deserialize(message.getBody()));
		TaskLog taskLog = JSON.parseObject(str, TaskLog.class);
		LogUtil.info(Logger, taskLog.getHost() + "  " + taskLog.getJobCode() + "  " + taskLog.getTimestamp() + "    " + taskLog.getLevel() + "  " + taskLog.getLogger() + "  " + taskLog.getMessage());
		System.out.println(taskLog.getHost() + "  " + taskLog.getJobCode() + "  " + taskLog.getTimestamp() + "    " + taskLog.getLevel() + "  " + taskLog.getLogger() + "  " + taskLog.getMessage());
		if (StringUtils.isNotEmpty(taskLog.getThrowable())) {
			LogUtil.error(Logger, taskLog.getThrowable());
		}
		//elasticSearchService.createIndex("logs", "job_log", null, str);
		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
	}

	public static void main(String[] args) {
		TaskLog taskLog = new TaskLog();
		taskLog.setJobCode("371452943015111097798243");
		taskLog.setHost("321");
		taskLog.setSource("cba");
		ElasticSearchService elasticSearchService = new ElasticSearchServiceImpl();
		elasticSearchService.createIndex("logs", "job_log", null, JSON.toJSONString(taskLog));


	}

}
